package com.tomorrow.runner.service;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.util.List;

import cn.sucun.android.log.Log;

/**
 * 传感器数据收集服务<br/>
 * 收集加速度传感器，重力传感器，陀螺仪传感器，距离传感器信息
 * Created by tomorrow on 16-4-20.
 */
public class SensorService extends Service implements SensorEventListener {
    private static final String TAG = SensorService.class.getName();
    SensorManager sensorManager;
    @Override
    public void onCreate() {
        super.onCreate();
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showSensorList();
        return super.onStartCommand(intent, flags, startId);
    }
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void showSensorList() {
        List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
        StringBuilder info = new StringBuilder();
        for (Sensor s : sensorList) {
            switch (s.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    boolean ok = sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_UI, 100);
                    Log.i(TAG,"ok="+ok);
                    info.append("加速度传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_AMBIENT_TEMPERATURE:
                    info.append("温度传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_GRAVITY:
                    ok = sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL, 1000);
                    Log.i(TAG,"ok="+ok);
                    info.append("重力传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_GYROSCOPE:
                    info.append("陀螺仪传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
                    info.append("无定标陀螺仪传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_HEART_RATE:
                    info.append("心跳传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_LIGHT:
                    info.append("光线传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_LINEAR_ACCELERATION:
                    info.append("线性加速传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    info.append("磁场传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_ORIENTATION:
                    info.append("方向传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_PRESSURE:
                    info.append("压力传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_PROXIMITY:
                    info.append("距离传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");

                    break;
                case Sensor.TYPE_RELATIVE_HUMIDITY:
                    info.append("相对湿度传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_ROTATION_VECTOR:
                    info.append("旋转矢量传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_SIGNIFICANT_MOTION:
                    info.append("重大事件传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_STEP_COUNTER:
                    info.append("记步传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                case Sensor.TYPE_STEP_DETECTOR:
                    info.append("步伐探测传感器\n")
                            .append(s.getVersion()+"\n")
                            .append(s.getVendor()+"\n");
                    break;
                default:
                    break;
            }
        }
        Log.i(TAG, info.toString());
    }
    float x = 0;
    float y = 0;
    float z = 0;
    @Override
    public void onSensorChanged(SensorEvent event) {
        Log.i(TAG, "onSensorChanged");
        Sensor sensor = event.sensor;
        switch (sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
            case Sensor.TYPE_GRAVITY:
                if (x == 0){
                    x = event.values[0];
                }
                if (y == 0){
                    y = event.values[1];
                }
                if (z == 0){
                    z = event.values[2];
                }
                Log.i(TAG, "x-axis = "+event.values[0]);
                Log.i(TAG, "y-axis = "+event.values[1]);
                Log.i(TAG, "z-axis = "+event.values[2]);
                break;
            default:
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.i(TAG, "onAccuracyChanged");
    }
}
